{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div id="tip_modal">
    <form class="layui-form p-4">
        <h3 class="pb-3">功能菜单/节点</h3>
        <table class="layui-table layui-table-form">
            <tr>
                <td class="layui-td-gray">关联源单据<font>*</font></td>
                <td>
                    <select name="correlation_order" id="correlation_order" lay-search lay-filter="correlation_order">
                        <option value="">请选择</option>
                        {volist name='planList' id='pl'}
                        <option value="{$pl.id}" {if isset($detail.correlation_order)&& $detail.correlation_order ==$pl.id} selected {/if}>{$pl.circulationNum}</option>
                        {/volist}
                    </select>
                </td>
                <td class="layui-td-gray">物料编码<font>*</font></td>
                <td>
                    <select name="material_dossier_id" id="material_dossier_id" lay-search lay-filter="material_dossier_id">
                        <option value="">请选择单据</option>
                    </select>
                </td>
                <td class="layui-td-gray">工序<font>*</font></td>
                <td>
                    <select name="craft_detail_id" id="craft_detail_id" lay-search lay-filter="craft_detail_id">
                        <option value="">请选择物料</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="layui-td-gray">物料名称</td>
                <td>
                    <span id="mdName"></span>
                </td>
                <td class="layui-td-gray">规格型号</td>
                <td>
                    <span id="specs"></span>
                </td>
                <td class="layui-td-gray">单位</td>
                <td>
                   <span id="unit"></span>
                </td>
            </tr>
            <tr>
                <td class="layui-td-gray">计价方式<font>*</font></td>
                <td>
                    <select name="pricing_mode" id="pricing_mode" lay-filter="pricing_mode">
                        <option value="">请选择</option>
                        {volist name='pricingMode' id='pm'}
                        <option value="{$pm}" {if isset($detail.pricing_mode)&& $detail.pricing_mode ==$pm} selected {/if}>{$pm}</option>
                        {/volist}
                    </select>
                </td>
                <td class="layui-td-gray">班组/个人<font>*</font></td>

                <td>
                    <select name="u_g_id" id="u_g_id">
                        <option value="">请选择计价方式</option>
                    </select>
                </td>
                <td class="layui-td-gray">单据日期<font>*</font></td>
                <td>
                    <input type="text" class="layui-input" value="{$detail.orderDate|default=$defalutTime}" readonly name="orderDate" id="orderDate" placeholder="选择时间"
                           required>
                </td>
            </tr>
            <tr>
                <td class="layui-td-gray">合格品数量<font>*</font></td>
                <td>
                    <input type="text" value="{$detail.qualifiedNum|default=''}" class="layui-input " maxlength='9' required
                           lay-verify="required" onkeyup="digitalFiltering(this,0)" placeholder="请输入数量"
                           autocomplete="off" min="0" max="999999" name="qualifiedNum">
                </td>
                      <td class="layui-td-gray">工废数量<font>*</font></td>
                <td>
                    <input type="text" value="{$detail.passNum|default=''}" class="layui-input " maxlength='9' required
                           lay-verify="required" onkeyup="digitalFiltering(this,0)" placeholder="请输入数量"
                           autocomplete="off" min="0" max="999999" name="passNum">
                </td>
                <td class="layui-td-gray">料废数量<font>*</font></td>
                <td>
                    <input type="text" value="{$detail.wasteNum|default=''}" class="layui-input " maxlength='9' required
                           lay-verify="required" onkeyup="digitalFiltering(this,0)" placeholder="请输入数量"
                           autocomplete="off" min="0" max="999999" name="wasteNum">
                </td>

            </tr>
            <tr>
                <td class="layui-td-gray">是否尾工序<font>*</font>
                </td>
                <td>
                    <select name="is_end" >
                        {foreach [['id'=>1,'name'=>'是'],['id'=>2,'name'=>'否']] as $ie}
                        <option value="{$ie.id}" {if isset($detail.is_end)&&$detail.is_end==$ie.id } selected {/if}>{$ie.name}</option>
                        {/foreach}
                    </select>

                </td>
                <td class="layui-td-gray">状态<font>*</font>
                </td>
                <td>
                    <select name="status" disabled>
                        {foreach [['id'=>1,'name'=>'草稿'],['id'=>2,'name'=>'待确认'],['id'=>3,'name'=>'已确认']] as $st}
                        <option value="{$st.id}" {if isset($detail.status)&&$detail.status==$st.id } selected {/if}>{$st.name}</option>
                        {/foreach}
                    </select>

                </td>

            </tr>
        </table>
        <div class="py-3">
            <input type="hidden" name="id" value="{$id}">
            <input type="hidden" id="d_road_id" name="d_road_id" value="{$detai.d_road_id|default=0}">
            {if !isset($detail.status)||$detail.status==1}
            <button class="layui-btn layui-btn-normal" type="button" id="sub" lay-submit="" lay-filter="webform1">保存 </button>
            {/if}
            {if isset($detail.status)&&$detail.status==1}
            <button type="button" class="layui-btn layui-btn-normal" onclick="onSub()">提交</button>
            {/if}
            {if isset($detail.status)&&$detail.status==2}
            <button type="button" class="layui-btn layui-btn-normal" onclick="onSure()">确认</button>
            {/if}
            {if isset($detail.status)&&$detail.status==3}
            <button type="button" class="layui-btn layui-btn-danger" onclick="cancel()">撤销</button>
            {/if}
        </div>
    </form>
</div>

{/block}
<!-- /主体 -->
{block name="script"}
<script>

    const moduleInit = ['tool'];

    let isSub = 2;
    let d_cid="{$detail.correlation_order|default=''}";
    let d_mid="{$detail.material_dossier_id|default=''}";
    let d_mType="{$detail.pricing_mode|default=''}";
    let d_road_id="{$detail.d_road_id|default=''}";
    function gouguInit() {
        var form = layui.form, tool = layui.tool,laydate=layui.laydate;
        //监听提交
        form.on('submit(webform1)', function (data) {
            let callback = function (e) {
                isSub = 2;
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            }
            if (isSub == 2) {
                isSub = 1
                tool.post("{:url('add')}", data.field, callback);
            }
            return false;
        });
        //动态获取联动
        var danjuid='';
        form.on('select(correlation_order)', function (data) {
            var cid=data.value;
            danjuid=cid;
            var optionStr=' <option value="">请选择单据</option>';
            $("#material_dossier_id").empty();
            $("#craft_detail_id").empty();
            if (cid!=''){
                getBarCode(cid);
            }else {
                $("#material_dossier_id").append(optionStr);
                $("#craft_detail_id").append(optionStr);
                $('#mdName').html('');
                $('#specs').html('');
                $('#unit').html('');
                form.render();
            }


        });
        form.on('select(material_dossier_id)', function (data) {
            var mid=data.value;
            d_road_id = $(data.elem).find('option:selected').attr('title');
            $('#d_road_id').val(d_road_id)
            var optionStr=' <option value="">请选择物料</option>';
            $("#craft_detail_id").empty();
            if (mid!=''&&danjuid!=''){
                getgongxu(mid,danjuid);
            }else {
                $("#craft_detail_id").append(optionStr);
                $('#mdName').html('');
                $('#specs').html('');
                $('#unit').html('');
                form.render();
            }


        });
        form.on('select(pricing_mode)', function (data) {
            var mode=data.value;
            var optionStr=' <option value="">请选择计价方式</option>';
            $("#u_g_id").empty();
            if (mode!=''){
                getPmode(mode)
            }else {
                $("#u_g_id").append(optionStr);
                form.render();
            }


        });
        //弹窗
        laydate.render({
            elem: '#orderDate'
            , type: 'date',
        });
        if (d_cid!=''){
            getBarCode(d_cid);
        }
        if (d_mid!=''){
            getgongxu(d_mid,d_cid);
        }
        if (d_mType!=''){
            getPmode(d_mType)
        }

    }
</script>
<script>
    function getBarCode(cid){
        $("#material_dossier_id").empty();
        $("#craft_detail_id").empty();
        var optionStr=' <option value="">请选择单据</option>';
        $.ajax({
            url:'/home/api/getProductionPlanDetail',
            type:'post',
            dataType:'json',
            data:{id:cid},
            success:function (res) {
                console.log('res',res)
                if (res.code==0){
                    let data=res.data;
                    let d_m_d_id="{$detail.material_dossier_id|default=''}"
                    let seStr='';
                    for(var i=0;i<data.length;i++){
                        if (d_m_d_id==data[i].material_dossier_id){
                            seStr='selected'
                        }
                        optionStr+=" <option value='"+data[i].material_dossier_id+"' title='"+data[i].craft_route_id+"' "+seStr+">"+data[i].bar_code+"</option>";
                    }
                }else {
                    optionStr+=" <option value=''>暂无数据</option>";
                }
                $("#material_dossier_id").append(optionStr);
                form.render();
            }

        })
    }
    function getgongxu(mid,danjuid){
        $("#craft_detail_id").empty();
        var optionStr=' <option value="">请选择物料</option>';
        $.ajax({
            url:'/home/api/getGx',
            type:'post',
            dataType:'json',
            data:{mid:mid,danjuid:danjuid,route_id:d_road_id},
            success:function (res) {
                console.log('res',res)
                if (res.code==0){
                    let mdInfo=res.data.mdInfo;
                    $('#mdName').html(mdInfo.name);
                    $('#specs').html(mdInfo.specs);
                    $('#unit').html(mdInfo.unit);
                    let data=res.data.list;
                    let d_c_d_id="{$detail.craft_detail_id|default=''}"
                    let seStr='';
                    for(var i=0;i<data.length;i++){
                        if (d_c_d_id==data[i].id){
                            seStr='selected'
                        }
                        optionStr+=" <option value='"+data[i].id+"' "+seStr+">"+data[i].craftName+"</option>";
                    }
                }else {
                    optionStr+=" <option value=''>暂无数据</option>";
                }
                $("#craft_detail_id").append(optionStr);
                form.render();

            }

        })
    }
    function getPmode(mode){
        $("#u_g_id").empty();
        var optionStr=' <option value="">请选择计价方式</option>';
        $.ajax({
            url:'/home/api/getPricingMode',
            type:'post',
            dataType:'json',
            data:{type:mode},
            success:function (res) {
                if (res.code==0){
                    optionStr='';
                    let data=res.data;
                    let d_u_g_id="{$detail.u_g_id|default=''}";
                    let seStr='';
                    for(var i=0;i<data.length;i++){
                        if (d_u_g_id==data[i].id){
                            seStr='selected'
                        }
                        optionStr+=" <option value='"+data[i].id+"' "+seStr+">"+data[i].name+"</option>";
                    }
                }else {
                    optionStr+=" <option value=''>暂无数据</option>";
                }
                $("#u_g_id").append(optionStr);
                form.render();

            }

        })
    }
    let is_sure=2;
    function onSure() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }
        if (is_sure==1){
            return;
        }
        is_sure=1;
        $.ajax({
            url:"{:url('onSure')}",
            type:'post',
            dataType:'json',
            data:{id:id},
            success:function (res) {
                is_sure=2;
                layer.msg(res.msg)
                if (res.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },
            error(e){
                layer.msg('网络错误，请稍后重试')
                is_sure=2
            }
        })
    }
    let is_sub=2;
    function onSub() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }
        if (is_sub==1){
            return;
        }
        is_sub=1;
        $.ajax({
            url:"{:url('confirm')}",
            type:'post',
            dataType:'json',
            data:{id:id},
            success:function (res) {
                is_sub=2;
                layer.msg(res.msg)
                if (res.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },
            error(e){
                layer.msg('网络错误，请稍后重试')
                is_sub=2
            }
        })
    }
    function cancel() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }
        layer.confirm('确定要撤销吗？', {
            btn: ['确定','取消'] //按钮
        }, function()
        {
            layer.closeAll('dialog');
            $.ajax({
                type: "post",
                url: "{:url('onCancel')}",
                data: {
                    "id": id
                },
                success : function(data){
                    layer.msg(data.msg);
                    if (data.code==0){
                        setTimeout(function () {
                            parent.parent.location.reload();
                        }, 1000);
                    }
                },
                error(e){
                    layer.msg('网络错误,请稍后再试');
                }
            });
        });
    }

</script>
{/block}
<!-- /脚本 -->